<template>
    <div>
        <div class="form-wrap">
            <el-form ref="form" :model="form" :rules="rules" label-width="100px">
                <el-form-item label="当前版本号">
                    <span>{{ maxVersion }}</span>
                </el-form-item>
                <el-form-item label="版本大小" prop="versionScale">
                    <el-radio v-model="form.versionScale" :label="0">主版本号</el-radio>
                    <el-radio v-model="form.versionScale" :label="1">次版本号</el-radio>
                    <el-radio v-model="form.versionScale" :label="2">修订号</el-radio>
                </el-form-item>
                <el-form-item label="下个版本号">
                    <span>{{ nextVersion }}</span>
                </el-form-item>
                <!-- <el-form-item label="业务名" prop="businessName">
                    <el-input v-model="form.businessName"></el-input>
                </el-form-item> -->
            </el-form>
        </div>
    </div>
</template>

<script>
import { findProjects, findBranchsByProject } from './api/index.js'

export default {
    name: 'publish',
    data () {
        return {
            form: {
                versionScale: ''
                // businessName: ''
            },
            projectsOptions: [],
            versionArr: [],
            maxVersion: ''
            // branchsOptions: [],
            // branchs: [],
            // isLoading: false,
            // isPublishLoading: false,
            // active: 0,
            // rules: {
            //     project: [
            //         { required: true, message: '此项为必填项！' }
            //     ],
            //     version: [
            //         { required: true, message: '此项为必填项！' }
            //     ],
            //     isPublish: [
            //         { required: true, message: '此项为必填项！' }
            //     ]
            // },
            // testBranchName: '',
            // publishBranchName: '',
            // checklistVisible: false,
            // checklist: [],
            // checklistOptions: [
            //     { label: '请求前缀配置确认（api文件夹下面，config.js文件中production配置）', value: '1' },
            //     { label: 'Master分支已经合并到当前版本', value: '2' }
            // ]
        }
    },
    computed: {
        nextVersion () {
            let maxVersionArr = this.maxVersion.split('.')
            if (this.form.versionScale !== '') {
                maxVersionArr[this.form.versionScale] = maxVersionArr[this.form.versionScale] * 1 + 1
                for (let i = this.form.versionScale * 1 + 1; i < maxVersionArr.length; i++) {
                    maxVersionArr[i] = 0
                }
                return maxVersionArr.join('.')
            } else {
                return ''
            }
        }
    },
    methods: {
        // 获取前端所有版本
        async getProjects () {
            const { data } = await findProjects()
            this.projectsOptions = data.map(item => ({
                label: item.name,
                value: item.id
            }))
        },
        async getBranchs (projectId) {
            const { data } = await findBranchsByProject({
                projectId
            })
            this.branchs = data
            this.branchsOptions = data.filter(item => item.name.indexOf('feature') !== -1).map(item => {
                const nameArr = item.name.split('_')
                if (nameArr.length < 2) {
                    return null
                }
                return {
                    label: nameArr[1],
                    value: item.name
                }
            })
        },
        getAllVersion () {
            let _this = this
            this.projectsOptions.forEach(async item => {
                const { data } = await findBranchsByProject({
                    projectId: item.value,
                    search: 'feature'
                })
                data.forEach(obj => {
                    let name = obj.name
                    let nameArr = name.split('_')
                    if (nameArr.length >= 2 && !/[^0-9\.]/.test(nameArr[1])) {
                        _this.versionArr.push(nameArr[1])
                        if (_this.maxVersion === '') {
                            _this.maxVersion = nameArr[1]
                        } else {
                            let [ preVersion1, preVersion2, preVersion3, preVersion4 ] = _this.maxVersion.split('.')
                            let [ currVersion1, currVersion2, currVersion3, currVersion4 ] = nameArr[1].split('.')
                            if (preVersion1 * 1 < currVersion1 * 1) {
                                _this.maxVersion = nameArr[1]
                            } else if (preVersion1 * 1 === currVersion1 * 1) {
                                if (preVersion2 * 1 < currVersion2 * 1) {
                                    _this.maxVersion = nameArr[1]
                                } else if (preVersion2 * 1 === currVersion2 * 1) {
                                    if (preVersion3 * 1 < currVersion3 * 1) {
                                        _this.maxVersion = nameArr[1]
                                    } else if (preVersion3 * 1 === currVersion3 * 1) {
                                        if (preVersion4 * 1 < currVersion4 * 1) {
                                            _this.maxVersion = nameArr[1]
                                        }
                                    }
                                }
                            }
                        }
                    }
                })
            })
        }
    },
    async mounted () {
        await this.getProjects()
        this.getAllVersion()
    }
}
</script>

<style lang="scss" scoped>
.el-checkbox+.el-checkbox {
    margin-top: 20px;
    margin-left: 0;
}
.el-checkbox {
    display: block;
}
.step-wrap {
    margin-bottom: 30px;
    padding: 10px;
}
/deep/ .el-checkbox, .el-checkbox__input {
    white-space: normal;
    word-break: break-all;
}
/deep/ .el-checkbox__label {
    display: inline;
}
</style>
